<template>
  <el-main>
    <!-- 搜索栏 -->
    <el-form ref="searchForm" :model="searchForm" :inline="true" size="small">
      <el-form-item prop="teacherName">
        <el-input v-model="searchForm.teacherName" placeholder="请输入教师名称" />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button style="color:#FF7670;" icon="el-icon-close" @click="resetBtn">重置</el-button>
        <el-button v-permission="['sys:teacherList:add']" type="primary" icon="el-icon-plus" @click="addBtn">新增</el-button>
      </el-form-item>
    </el-form>
    <sys-dialog
      :title="addDialog.title"
      :height="addDialog.height"
      :width="addDialog.width"
      :visible="addDialog.visible"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <el-form ref="addModel" :model="addModel" :rules="rules" label-width="80px" :inline="false" size="small">
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="teacherName" label="老师姓名">
                <el-input v-model="addModel.teacherName" />
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="sex" label="性别">
                <el-radio-group v-model="addModel.sex">
                  <el-radio :label="'0'">男</el-radio>
                  <el-radio :label="'1'">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="phone" label="电话">
                <el-input v-model="addModel.phone" />
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="teacherNum" label="教师编号">
                <el-input v-model="addModel.teacherNum" />
              </el-form-item></el-col>
          </el-row>

          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="password" label="密码">
                <el-input v-model="addModel.password" />
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="roleId" label="所属角色">
                <el-select
                  v-model="addModel.roleId"
                  style="width:100%"
                  placeholder="请选择角色"
                >
                  <el-option
                    v-for="item in rolesList"
                    :key="item.roleId"
                    :label="item.roleName"
                    :value="item.roleId"
                  />
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>

        </el-form>
      </div>
    </sys-dialog>
    <!-- 教师表格列表 -->
    <el-table :data="tableList" :height="tableHeight" border stripe>
      <el-table-column prop="teacherName" label="教师名称" />
      <el-table-column prop="teacherNum" label="教师编号" />
      <el-table-column prop="sex" label="性别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex === '0'" type="danger" size="small">男</el-tag>
          <el-tag v-if="scope.row.sex === '1'" type="blue" size="small">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="电话" />
      <el-table-column v-if="$checkPermission(['sys:teacherList:edit', 'sys:teacherList:reset', 'ssys:teacherList:delete'])" label="操作" align="center" width="320">
        <template slot-scope="scope">
          <el-button v-permission="['sys:teacherList:edit']" type="primary" size="small" icon="el-icon-edit" @click="editBtn(scope.row)">修改</el-button>
          <el-button v-permission="['sys:teacherList:reset']" type="warning" size="small" icon="el-icon-edit" @click="resetPasswordBtn(scope.row)">重置密码</el-button>
          <el-button v-permission="['sys:teacherList:delete']" type="danger" size="small" icon="el-icon-close" @click="deleteBtn(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件 -->
    <el-pagination
      :current-page.sync="searchForm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="searchForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="searchForm.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />

  </el-main>

</template>

<script>
import SysDialog from '@/components/dialog/SysDialog.vue'
import { addTeacherApi, deleteTeacherApi, editTeacherApi, getTeacherListApi, getRoleApi, getRoleByIdApi, resetPasswordApi } from '@/api/teacher'
export default {
  components: {
    SysDialog
  },
  data() {
    return {
      // 搜索参数设置
      searchForm: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        teacherName: ''
      },
      // 弹出框设置
      addDialog: {
        title: '',
        height: 300,
        width: 630,
        visible: false
      },
      // 绑定弹出框数据
      addModel: {
        teacherId: '',
        teacherName: '',
        sex: '',
        phone: '',
        teacherNum: '',
        password: '',
        roleId: '',
        type: ''
      },
      // 弹出框表单数据验证
      rules: {
        teacherName: [{
          required: true,
          message: '请输入教师名称',
          trigge: 'blur'
        }],
        sex: [{
          required: true,
          message: '请选择性别'
        }],
        phone: [{
          required: true,
          message: '请输入手机号',
          trigge: 'blur'
        }, {
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigge: 'blur'
        }],
        teacherNum: [{
          required: true,
          message: '请输入教师编号',
          trigge: 'blur'
        }]
      },
      // 表格属性
      tableHeight: 0,
      // 表格数据
      tableList: [],
      // 角色列表
      rolesList: []
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 220
    })
  },
  methods: {
    async resetPasswordBtn(row) {
      const confirm = await this.$myconfirm('确认重置密码吗？重置后密码为【666666】')
      if (confirm) {
        const params = {
          teacherId: row.teacherId,
          password: '666666'
        }
        const res = await resetPasswordApi(params)
        if (res && res.code === 200) {
          this.$message.success(res.msg)
        }
      }
    },
    async getRoles() {
      const res = await getRoleApi()
      if (res && res.code === 200) {
        this.rolesList = res.data
        console.log(this.rolesList)
      }
    },
    // 页容量变化
    sizeChange(val) {
      this.searchForm.pageSize = val
      this.getList()
    },
    // 当前页变化
    currentChange(val) {
      this.searchForm.currentPage = val
      this.getList()
    },
    // 搜索按钮
    searchBtn() {
      this.getList()
    },
    // 重置按钮
    resetBtn() {
      this.searchForm.teacherName = ''
      this.searchForm.currentPage = 1
      this.searchForm.pageSize = 10
      this.getList()
    },
    // 新增按钮
    addBtn() {
      // 清空数据
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
        this.$emptyData(this.addModel)
      }
      // 获取角色列表
      this.getRoles()
      this.addDialog.title = '新增教师信息'
      this.addModel.type = '0'
      this.addDialog.visible = true
    },
    onClose() {
      this.addDialog.visible = false
    },
    onConfirm() {
      this.$refs.addModel.validate(async(valid) => {
        if (valid) {
          let res = null
          if (this.addModel.type === '0') {
            res = await addTeacherApi(this.addModel)
          } else {
            res = await editTeacherApi(this.addModel)
          }
          if (res && res.code === 200) {
            // 消息提示
            this.$message.success(res.msg)
            // 刷新列表
            this.getList()
            // 关闭弹窗
            this.addDialog.visible = false
          }
        }
      })
    },
    async editBtn(row) {
      // 清空数据
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
        this.$emptyData(this.addModel)
      }
      // 获取角色列表
      this.getRoles()
      this.$objCopy(row, this.addModel)
      const res = await getRoleByIdApi({
        teacherId: row.teacherId
      })
      if (res && res.code === 200) {
        if (res.data !== null) {
          this.addModel.roleId = res.data.roleId
        } else {
          this.addModel.roleId = ''
        }
      }
      this.addDialog.title = '编辑教师信息'
      this.addModel.type = '1'
      this.addDialog.visible = true
    },
    async deleteBtn(row) {
      const confirm = await this.$myconfirm('确认删除该条数据吗')
      if (confirm) {
        const res = await deleteTeacherApi({ teacherId: row.teacherId })
        if (res && res.code === 200) {
          this.$message.success(res.msg)
          this.getList()
        }
      }
    },
    async getList() {
      const res = await getTeacherListApi(this.searchForm)
      if (res && res.code === 200) {
        this.tableList = res.data.records
        this.searchForm.total = res.data.total
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
